package uzivatele;

import database.DatabazovaChyba;
import system.Launcher;
import ukoly.UkolControl;

/*@Author KrutinaJan@gmail.com*/
public class Uzivatel {

    final private int id;//id je dano databazi sql
    private String jmeno;
    private String prijmeni;
    private String email;
    private String popis;
    private String heslo;
    //private ArrayList<Uzivatel> podrizeni;
    private int rank;
    //popripade Dbservice
    private UkolControl ukol;

    /** Konstruktor pro vytvoreni noveho uzivatele
     * 
     * @param id unikatni id, obstara mysql server
     * @param jmeno jmeno uzivatele
     * @param prijmeni prijmeni uzivatele
     * @param email email uzivatele
     * @param popis popis uzivatele 
     * @param rank prava uzivatele:
     *      0 - user
     *      1 - manazer
     *      2 - administrator
     * @param heslo heslo noveho uzivatele
     */
    public Uzivatel(int id, String jmeno, String prijmeni, String email, String popis, int rank, String heslo) {
        this.id = id;
        this.jmeno = jmeno;
        this.prijmeni = prijmeni;
        this.email = email;
        this.popis = popis;
        this.rank = rank;
        this.heslo = heslo;
    }

    /**metoda slouzi k smazani uzivatele z databaze
     * 
     * @param id id uzivatele pro smazani
     */
    public void deleteUser(Uzivatel user) throws DatabazovaChyba {
        Launcher.getInstance().getDatabase().smazUzivatele(user);
    }

    /**metoda meni edituje aktualniho uzivatele na kterem je tato metoda volana
     * funguje i jako metoda createUser
     * @param jmeno 
     * @param prijmeni
     * @param email
     * @param popis
     * @param rank
     * @param heslo
     * @param switcher ->0 pro vytvoreni noveho uzivatele, -> 1 pro editaci
     * @throws DatabazovaChyba 
     */
    public Uzivatel editUser(String jmeno, String prijmeni, String email, String popis, int rank, String heslo, int prepinac) throws DatabazovaChyba {
        if (prepinac == 0) {
            Uzivatel u = new Uzivatel(-1, jmeno, prijmeni, email, popis, rank, heslo);
            u = Launcher.getInstance().getDatabase().vytvorUzivatele(u);
            return u;
        } else {
            this.jmeno = jmeno;
            this.prijmeni = prijmeni;
            this.email = email;
            this.popis = popis;
            this.heslo = heslo;
            this.rank = rank;
            Launcher.getInstance().getDatabase().ulozUzivatele(this);
            return this;
        }
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHeslo() {
        return heslo;
    }

    public void setHeslo(String heslo) {
        this.heslo = heslo;
    }

    public String getJmeno() {
        return jmeno;
    }

    public void setJmeno(String jmeno) {
        this.jmeno = jmeno;
    }

    public String getPopis() {
        return popis;
    }

    public void setPopis(String popis) {
        this.popis = popis;
    }

    public String getPrijmeni() {
        return prijmeni;
    }

    public void setPrijmeni(String prijmeni) {
        this.prijmeni = prijmeni;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public UkolControl getUkol() {
        return ukol;
    }

    public void setUkol(UkolControl ukol) {
        this.ukol = ukol;
    }

    public int getId() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Uzivatel other = (Uzivatel) obj;
        if (this.id != other.id) {
            return false;
        }
        if ((this.jmeno == null) ? (other.jmeno != null) : !this.jmeno.equals(other.jmeno)) {
            return false;
        }
        if ((this.prijmeni == null) ? (other.prijmeni != null) : !this.prijmeni.equals(other.prijmeni)) {
            return false;
        }
        if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
            return false;
        }
        if ((this.popis == null) ? (other.popis != null) : !this.popis.equals(other.popis)) {
            return false;
        }
//        if ((this.heslo == null) ? (other.heslo != null) : !this.heslo.equals(other.heslo)) {
//            return false;
//        }
        if (this.rank != other.rank) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 89 * hash + this.id;
        hash = 89 * hash + (this.jmeno != null ? this.jmeno.hashCode() : 0);
        hash = 89 * hash + (this.prijmeni != null ? this.prijmeni.hashCode() : 0);
        hash = 89 * hash + (this.email != null ? this.email.hashCode() : 0);
        hash = 89 * hash + (this.popis != null ? this.popis.hashCode() : 0);
//        hash = 89 * hash + (this.heslo != null ? this.heslo.hashCode() : 0);
        hash = 89 * hash + this.rank;
        return hash;
    }
}